package com.morpheus.zookeeper.config;

import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;

import com.morpheus.zookeeper.api.client.ZookeeperClient;
import com.morpheus.zookeeper.listener.ZookeeperStateListener;

@Configuration
@ConditionalOnClass({ CuratorFramework.class })
@EnableConfigurationProperties({ ZookeeperProperties.class })
@Order(10)
public class ZookeeperAutoConfiguration {
	private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperAutoConfiguration.class);

	public ZookeeperAutoConfiguration() {
	}

	@Bean
	//@Scope(scopeName = "prototype")
	public CuratorFramework curatorFramework(ZookeeperProperties zookeeperProperties) {
		ZookeeperStateListener zookeeperStateListener = new ZookeeperStateListener();
		CuratorFramework curatorFramework = zookeeperStateListener.connect(zookeeperProperties);
		return curatorFramework;
	}

	@Bean
	public ZookeeperClient zookeeperClient(CuratorFramework curatorFramework, ZookeeperProperties zookeeperProperties) {
		ZookeeperClient zookeeperClient = new ZookeeperClient(curatorFramework, zookeeperProperties);
		return zookeeperClient;
	}
}
